package controller

import (
	"advertising/config/mysql"
	"advertising/internal/app/web/service"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type SupportActivity struct{}

// Create 创建支持活动
func (SupportActivity) Create(c *gin.Context) {
	var params struct {
		Name        string `json:"name"`
		StartTime   int64  `json:"start_time"`
		EndTime     int64  `json:"end_time"`
		MaterialIDs string `json:"material_ids"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	userID := c.GetString("user_id")
	username := c.GetString("username")
	db := mysql.NewDB()
	id, err := service.SupportActivityService{}.Create(c.Request.Context(), db, params.Name, params.StartTime, params.EndTime, params.MaterialIDs, userID, username)
	response.JSON(c, gin.H{
		"id": id,
	}, err)
}

// Update 更新支持活动
func (SupportActivity) Update(c *gin.Context) {
	var params struct {
		ID          string `json:"id"`
		Name        string `json:"name"`
		StartTime   int64  `json:"start_time"`
		EndTime     int64  `json:"end_time"`
		MaterialIDs string `json:"material_ids"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := service.SupportActivityService{}.Update(c.Request.Context(), db, params.ID, params.Name, params.StartTime, params.EndTime, params.MaterialIDs)
	response.JSON(c, nil, err)
}

// Delete 删除支持活动
func (SupportActivity) Delete(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := service.SupportActivityService{}.Delete(c.Request.Context(), db, params.ID)
	response.JSON(c, nil, err)
}

// Detail 获取活动详情
func (SupportActivity) Detail(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	data, err := service.SupportActivityService{}.Detail(c.Request.Context(), db, params.ID)
	response.JSON(c, data, err)
}

// List 获取支持活动列表
func (SupportActivity) List(c *gin.Context) {
	var params struct {
		Name       string `json:"name"`
		ID         string `json:"id"`
		Status     int8   `json:"status"`
		CreateUser string `json:"create_user"`
		StartTime  int64  `json:"start_time"`
		EndTime    int64  `json:"end_time"`
		Page       int    `json:"page"`
		Limit      int    `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	list, count, err := service.SupportActivityService{}.List(
		c.Request.Context(),
		db,
		params.Name,
		params.ID,
		params.Status,
		params.CreateUser,
		params.StartTime,
		params.EndTime,
		params.Page,
		params.Limit,
	)
	response.JSON(c, gin.H{
		"count": count,
		"list":  list,
	}, err)
}

// ChangeStatus 修改活动状态
func (SupportActivity) ChangeStatus(c *gin.Context) {
	var params struct {
		ID     string `json:"id"`
		Status int8   `json:"status"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := service.SupportActivityService{}.ChangeStatus(c.Request.Context(), db, params.ID, params.Status)
	response.JSON(c, nil, err)
}

// SameNameExist 检查同名活动是否存在
func (SupportActivity) SameNameExist(c *gin.Context) {
	var params struct {
		Name     string `json:"name"`
		ExceptID string `json:"except_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	count := service.SupportActivityService{}.SameNameExist(c.Request.Context(), db, params.Name, params.ExceptID)
	response.JSON(c, gin.H{
		"count": count,
	}, nil)
}

// GetActivityListByMaterialIDs 根据素材ID获取关联的活动列表
func (SupportActivity) GetActivityListByMaterialIDs(c *gin.Context) {
	var params struct {
		MaterialID string `json:"material_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	list, err := service.SupportActivityService{}.GetActivityListByMaterialIDs(c.Request.Context(), db, params.MaterialID)
	response.JSON(c, list, err)
}

// GetMaterialIDsList 获取活动关联的素材ID列表
func (SupportActivity) GetMaterialIDsList(c *gin.Context) {
	var params struct {
		IDs []string `json:"ids"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	list, err := service.SupportActivityService{}.GetMaterialIDsList(c.Request.Context(), db, params.IDs)
	response.JSON(c, list, err)
}

// SetMaterialIDs 更新活动关联的素材 ID
func (SupportActivity) SetMaterialIDs(c *gin.Context) {
	var params struct {
		ID          string `json:"id"`
		MaterialIDs string `json:"material_ids"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)

	db := mysql.NewDB()
	err := service.SupportActivityService{}.
		SetMaterialIDs(c.Request.Context(), db, params.ID, params.MaterialIDs)
	response.JSON(c, nil, err)
}
